// 群组权限工具类
export default {
  // 获取当前用户在指定群组中的角色信息
  getCurrentUserRole(store, groupId = null) {
    const currentUserId = store.state.userStore.userInfo.id;
    const activeGroup = groupId 
      ? store.state.groupStore.groups.find(g => g.id === groupId)
      : store.state.groupStore.activeGroup;

    if (!activeGroup) return null;

    return {
      isOwner: String(currentUserId) == String(activeGroup.ownerId),
      isAdmin: activeGroup.groupMembers?.find(m => 
        String(m.userId) == String(currentUserId) && m.isAdmin == 'Y'
      ) != null,
      isMember: activeGroup.groupMembers?.find(m => 
        String(m.userId) == String(currentUserId)
      ) != null
    };
  },

  // 检查是否有管理权限（群主或管理员）
  hasManagePermission(store, memberId = null) {
    const currentUserId = store.state.userStore.userInfo.id;
    const activeGroup = store.state.groupStore.activeGroup;
    
    if (!activeGroup || !activeGroup.groupMembers) return false;

    // 如果传入了memberId,则判断该成员是否是群主或管理员
    const targetId = memberId || currentUserId;
    
    // 判断是否是群主
    if (String(targetId) == String(activeGroup.ownerId)) {
      return true;
    }
    // 判断是否是管理员
    const member = activeGroup.groupMembers.find(m => String(m.userId) == String(targetId));
    return member?.isAdmin === 'Y';
  },

  // 检查是否被禁言(包括个人禁言和全体禁言)
  isProhibited(store) {
    const currentUserId = store.state.userStore.userInfo.id;
    const targetId = store.state.chatStore.activeChat.targetId;
    const activeGroup = store.state.groupStore.groups.find(g => g.id == targetId);
    if (!activeGroup) return false;

    // 群主不受禁言限制
    if (String(currentUserId) == String(activeGroup.ownerId)) {
      return false;
    }

    const currentMember = activeGroup.groupMembers?.find(m => String(m.userId) == String(currentUserId));
    // 检查个人禁言
    if (currentMember?.prohibition === 'Y') {
      return true;
    }
    // 管理员不受禁言限制
    if (currentMember?.isAdmin === 'Y') {
      return false;
    }
    // 检查全体禁言
    if (activeGroup.prohibition === 'Y') {
      return true;
    }
    return false;
  }
} 